<?php
namespace Modules\ShopApi\Models; 
use Illuminate\Support\Facades\DB;
use Modules\ShopApi\Services\ShopUserService;
use Modules\ShopApi\Models\ShopProductSpec as ShopProductSpecModel;
use Modules\AuthAdmin\Services\CommonService;
class ShopCart extends BaseApiModel
{	
	/**
     * @name  加入购物车 
     */
	public function add_cart($data){
		$user_info = (new ShopUserService())->my();
		$info = $this->select('id','num')->where(['product_id'=>$data['product_id'],'specs_id'=>$data['specs_id'],'user_id'=>$user_info['id'],'is_delete'=>0])->first();
		$res = false;
		$money = ShopProductSpecModel::where(['id'=>$data['specs_id']])->value('money');
		if($info){
			$info = $info->toArray();
			if(ShopProductSpecModel::where(['id'=>$data['specs_id']])->value('stock')>($info['num']+$data['num'])){
				$res = $this->where(['id'=>$info['id']])->update([
					'num'=>$info['num']+$data['num'],
					'money'=>$money,
					'updated_at'=>date('Y-m-d H:i:s')
				]);
			}else{
				return $this->api_error(['msg'=>'库存不足！']);	
			}
		}else{
			if(ShopProductSpecModel::where(['id'=>$data['specs_id']])->value('stock')>$data['num']){
				$insert_data = [
					'user_id'=>$user_info['id'],
					'product_id'=>$data['product_id'],
					'num'=>$data['num'],
					'specs_id'=>$data['specs_id'],
					'money'=>$money,
					'created_at'=>date('Y-m-d H:i:s'),
				];
				if($data['specs']){
					$insert_data['specs'] = $data['specs'];
				}
				$res = $this->insert($insert_data);
			}else{
				return $this->api_error(['msg'=>'库存不足！']);	
			}
		}
		if($res){
			return $this->api_success([
				'msg'=>'加入购物车成功！',
				'data'=>[ 
					'cart_num'=>$this->where(['user_id'=>$user_info['id'],'is_delete'=>0])->sum('num')
				]
			]);
		}else{
			return $this->api_error();
		}
	}
	/**
     * @name  购物车数量 
     */
	public function get_cart_sum(){
		$user_info = (new ShopUserService())->my();
		if($user_info){
			return $this->api_success([
				'data'=>[ 
					'cart_num'=>$this->where(['user_id'=>$user_info['id'],'is_delete'=>0])->sum('num')
				]
			]);
		}else{
			return $this->api_success([
				'data'=>[ 
					'cart_num'=>0
				]
			]);
		}
		
	}
	/**
     * @name 关联商品
     */
    public function product_to()
    {
        return $this->belongsTo('Modules\ShopApi\Models\ShopProduct','product_id','id');
    }
	/**
     * @name 关联规格
     */
    public function specs_to()
    {
        return $this->belongsTo('Modules\ShopApi\Models\ShopProductSpec','specs_id','id');
    }
	/**
     * @name  购物车列表 
     */
	public function get_cart_list(){
		$user_info = (new ShopUserService())->my();
		if($user_info){
			$list = $this->select('id','product_id','user_id','num','specs_id','specs','status')
				->whereHas('product_to',function($query){
					$query->where(['status'=>1,'is_delete'=>0]);
				})
				->with([
					'product_to'=>function($query){
						$query->select('id','name','image_id');
					},
					'product_to.image_one'=>function($query){
						$query->select('id','url','open');
					},
					'specs_to'=>function($query){
						$query->select('id','money','stock');
					}
				])
				->orderBy('id','desc')
				->where(['user_id'=>$user_info['id'],'is_delete'=>0])
				->get()
				->toArray();
			return $this->api_success(['data'=>$this->cart_list_data($list)]);
		}else{
			return $this->api_success(['data'=>[]]);
		}
	}
	/**
     * @name  购物车列表数据处理
     */
	private function cart_list_data($info){
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		$money_all = '0.00';
		$num_all = 0;
		$num_all_m = 0;
		$status_all = 1;
		foreach($info as $k=>$v){
			$info[$k]['name'] 		= $v['product_to']['name'];
			$info[$k]['money'] 		= $v['specs_to']['money'];
			$info[$k]['stock'] 		= $v['specs_to']['stock'];
			if($v['product_to']['image_one']['open'] == 1){
				$info[$k]['image_url'] = $http . $v['product_to']['image_one']['url'];
			}else{
				$info[$k]['image_url'] = $v['product_to']['image_one']['url'];
			}
			if($v['status']){
				$money_all = $money_all + $v['specs_to']['money'] * $v['num'];
			}else{
				$status_all = 0;
			}
			if($v['status']){
				$num_all_m = $num_all_m + $v['num'];
			}
			$num_all = $num_all + $v['num'];
			unset($info[$k]['user_id'],$info[$k]['specs_id'],$info[$k]['product_to'],$info[$k]['specs_to']);
		}
		return [
			'list'=>$info,
			'money_all'=>sprintf("%.2f",$money_all),
			'num_all'=>$num_all,
			'status_all'=>$status_all,
			'num_all_m'=>$num_all_m
		];
	}
	/**
     * @name  单选
     */
	public function set_cart_status($id){
		$user_info = (new ShopUserService())->my();
		$status = 0;
		if($user_info){
			$status = $this->where(['user_id'=>$user_info['id'],'id'=>$id])->value('status')?0:1;
			if(!$this->where(['user_id'=>$user_info['id'],'id'=>$id])->update(['status' => $status])){
				return $this->api_error();
			}
		}
		$data = $this->cart_data($user_info);
		$data['status'] = $status;
		return $this->api_success([
			'data'=>$data
		]);
	}
	/**
     * @name  购物车数据处理
     */
	private function cart_data($user_info){
		$money_all = '0.00';
		$num_all = 0;
		$num_all_m = 0;
		$status_all = 0;
		if($user_info){
			$num_all = $this->where(['user_id'=>$user_info['id'],'is_delete'=>0])->sum('num');
			$num_all_m = $this->where(['status'=>1,'user_id'=>$user_info['id'],'is_delete'=>0,'status'=>1])->sum('num');
			$status_all = $this->where(['user_id'=>$user_info['id'],'is_delete'=>0,'status'=>0])->count();
			if($status_all){
				$status_all = 0;
			}else{
				$status_all = 1;
			}
			$money_all_arr = $this->where([
				'user_id'=>$user_info['id'],
				'status'=>1,
				'is_delete'=>0
			])->select('id','money','num')->get()->toArray();
			foreach($money_all_arr as $k=>$v){
				$money_all = $money_all + $v['money'] * $v['num'];
			}
		}
		return [
			'num_all_m'=>$num_all_m,
			'money_all' => sprintf("%.2f",$money_all),
			'num_all' => $num_all,
			'status_all' => $status_all
		];
	}
	/**
     * @name  全选全不选
     */
	public function set_cart_status_all(){
		$user_info = (new ShopUserService())->my();
		if($user_info){
			$status = $this->where(['user_id'=>$user_info['id'],'is_delete'=>0,'status'=>0])->count()?1:0;
			if(!$this->where(['user_id'=>$user_info['id'],'is_delete'=>0])->update(['status' => $status ])){
				return $this->api_error();
			}
		}
		$data = $this->cart_data($user_info);
		return $this->api_success([
			'data'=>$data
		]);
	}
	/**
     * @name  购物车减或加 
     */
	public function set_cart_subtract($data){
		$user_info = (new ShopUserService())->my();
		if($user_info){
			$specs_id = $this->where(['user_id'=>$user_info['id'],'is_delete'=>0])->value('specs_id');
			if(ShopProductSpecModel::where(['id'=>$specs_id])->value('stock')>=$data['num']){
				if(!$this->where(['id'=>$data['id']])->update([
					'num'=>$data['num'],
					'status'=>1,
					'updated_at'=>date('Y-m-d H:i:s')
				])){
					return $this->api_error();
				}
			}else{
				return $this->api_error(['msg'=>'库存不足！']);	
			}
		}
		$info = $this->cart_data($user_info);
		return $this->api_success([
			'data'=>$info
		]);
	}
	/**
     * @name  购物车删除 
     */
	public function cart_delete(){
		$user_info = (new ShopUserService())->my();
		if($this->where(['user_id'=>$user_info['id'],'status'=>1])->update(['is_delete' => 1 ])){
			return $this->api_success();
		}else{
			return $this->api_error();
		}
	}
}
